///<reference types="@i3yun/viewer" />
import { Container } from '../Container';
import { BaseGui } from "../Services/BaseGui";
import { DataService } from "../Services/DataService";
import { ModelManService } from '../Services/ModelManService';
import { ViewerService } from "../Services/ViewerService";
export class ModelManGui extends BaseGui {
    title = `模型管理[${ModelManGui.name}]`;
    ViewerService = Container.defineProperty(ViewerService);
    DataService = Container.defineProperty(DataService);
    ModelManService = Container.defineProperty(ModelManService);
    getData() {
        return this.ModelManService.getData();
    }
    async UpdateGui() {
        await this.ViewerService.ViewerPromise;
        let array = this.getData().modelLayers;
        let gui = this.gui;
        var fun = {
            addJob: () => {
                let jobid = window.prompt("job");
                if (jobid) {
                    let model = {
                        name: `name${array.length}`,
                        url: `jobs/${jobid}/output/main.hf`
                    };
                    array.push(model);
                    fun.data = array.length - 1;
                    fun._dataChanged();
                    this.updateViewer();
                }
            },
            addUrl: () => {
                let url = window.prompt("url");
                if (url) {
                    let model = { name: `name${array.length}`, url: url };
                    array.push(model);
                    fun.data = array.length - 1;
                    fun._dataChanged();
                    this.updateViewer();
                }
            },
            remove: () => {
                if (!window.confirm("确实要清除选中项吗？"))
                    return;
                let model = array[fun.data];
                array.splice(fun.data, 1);
                fun.data = fun.data < array.length ? fun.data : fun.data - 1;
                fun._dataChanged();
                this.updateViewer();
            },
            select: () => {
                let temp = this.ModelManService.getModel(array[fun.data]);
                if (temp) {
                    temp.selector.clearSelection();
                    temp.selector.setSelection([temp.getRootId()], 0);
                }
            },
            data: -1,
            dataName: "",
            dataUrl: "",
            dataDisplayName: "",
            _dataUrlChanged: () => {
                array[fun.data].url = fun.dataUrl;
                updateGUI();
                this.updateViewer();
            },
            _dataDisplayNameChanged: () => {
                array[fun.data].displayName = fun.dataDisplayName;
                updateGUI();
                this.updateViewer();
            },
            _dataOptions: () => {
                let options = {} as any;
                array.forEach((v, i: number) => {
                    const name = `[${i + 1}]${v.name} ${v.displayName}`;
                    options[name] = i;
                });
                return options;
            },
            _dataChanged: () => {
                let data = array[fun.data];
                fun.dataName = data.name;
                fun.dataUrl = data.url;
                // fun.dataHide = data.hide == undefined ? false : data.hide;
                // fun.dataTransparent = data.transparent == undefined ? false : data.transparent;
                updateGUI();
            },
            _dataNameChanged: () => {
                array[fun.data].name = fun.dataName;
                updateGUI();
                this.updateViewer();
            },
        }
        let updateGUI = () => {
            Array.from(gui.__controllers).forEach(_ => {
                gui.remove(_);
            });
            this.guiService.buildGUIByObject(gui, fun);
        }
        updateGUI();
    }
    Init() {
        super.Init();
        this.DataService.OnDataChanged.addEventListener(() => {
            this.updateViewer();
            this.Refresh();
        });
    }
    updateViewer() {
        return this.ModelManService.updateViewer();
    }
}